﻿using System;
using System.Activities.DurableInstancing;
using System.Runtime.DurableInstancing;
using Reactor.Environment;

namespace Reactor.Workflow.Persistence
{
    public static class InstanceStoreFactory
    {
        public static InstanceStore GetInstanceStore()
        {
            // TODO: Create mechanism to customize store location!
            var connectionString = Context.ServiceBus.Configuration.ConnectionData.DatabaseConnectionString;
            var store = new SqlWorkflowInstanceStore(connectionString);
            InstanceHandle handle = store.CreateInstanceHandle();
            InstanceView view = store.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
            handle.Free();
            store.DefaultInstanceOwner = view.InstanceOwner;

            return store;
        }
    }
}
